/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bitvision.tilescoloroptimizer;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;

/**
 *
 * @author bitvision
 */
public class TilesColorOptimizer {

    public static void main(String args[]) throws Exception {
        if (args.length!=2) {
            System.out.println("TilesColorOptimizer <input color file> <input tiles file>");
            System.exit(1);
        }

        
        List<Byte> outputBufferColors = new ArrayList<Byte>();
        List<Byte> outputBufferTiles = new ArrayList<Byte>();
        
        byte[] srcColorFile = Files.readAllBytes(Paths.get(args[0]));
        byte[] srcTilesFile = Files.readAllBytes(Paths.get(args[1]));
    
        byte previousBackgroundColor = getBackgroundColor(srcColorFile[0]);
        byte previousForegroundColor = getForegroundColor(srcColorFile[0]);

        //fixes 0s to 1s
        byte color = (byte) ((getBackgroundColor(srcColorFile[0]) << 4) | getForegroundColor(srcColorFile[0]));
        byte tile  = (byte)(srcTilesFile[0] & 0xFF);

        
        outputBufferColors.add(color);
        outputBufferTiles.add(tile);
        
        
        for (int i=1;i<srcColorFile.length;i++){
            //fixes 0s to 1s
            color = (byte) ((getBackgroundColor(srcColorFile[i]) << 4) | getForegroundColor(srcColorFile[i]));
            tile  = (byte)(srcTilesFile[i] & 0xFF); 
            
            //swap colors?
            if (previousBackgroundColor==getForegroundColor(srcColorFile[i]) && 
                previousForegroundColor==getBackgroundColor(srcColorFile[i]) ) {
                //swap
                color = (byte) ((getForegroundColor(srcColorFile[i]) << 4) | getBackgroundColor(srcColorFile[i]));
                if (previousBackgroundColor == previousForegroundColor) {
                    tile = (byte)0xFF;
                } else {
                    tile ^= 0xff; tile = (byte)(tile & 0xFF);
                }
                
            }
            
            outputBufferColors.add(color);
            outputBufferTiles.add(tile);
            
            previousBackgroundColor = getBackgroundColor(color);
            previousForegroundColor = getForegroundColor(color);
            
        }
        
        Byte[] finalbuffer = outputBufferColors.toArray(new Byte[outputBufferColors.size()]);
        byte[] finalarray = ArrayUtils.toPrimitive(finalbuffer);
        Path path = Paths.get(args[0]+".optimized");
        Files.write(path, finalarray);
        System.out.println("Writing " + args[0]+".optimized");
        
        finalbuffer = outputBufferTiles.toArray(new Byte[outputBufferTiles.size()]);
        finalarray = ArrayUtils.toPrimitive(finalbuffer);
        path = Paths.get(args[1]+".optimized");
        Files.write(path, finalarray);
        System.out.println("Writing " + args[1]+".optimized");
        
    }

    
    
    
    public static byte getBackgroundColor(byte val) {
        byte retval = (byte)(((val & 0xF0) >> 4) & 0xFF);
        if (retval==0){
            retval = 1;
        }
        return retval; 
    }

    public static byte getForegroundColor(byte val) {
        byte retval = (byte) ((val & 0x0F) & 0xFF);
        if (retval==0){
            retval = 1;
        }
        return retval; 
    }
    
    
}
